from apps.dbConnect.dbTransaction import dbTransactionCurd


def recursionListOrigin(db,data:list,sqlObjObj:dict,obj:dict):
    """

    :param db: 数据库连接
    :param data: 数据源列表
    :param sqlObjObj: 数据操作参数
    :param obj: 返回的数据
    :return:
    """
    # db.session.rollback()
    for itm in data:
        # print(type(itm))
        # print(itm['name'])
        if itm.get('sql',None) is None:
            return None,'数据错误不存在sql键'
        if itm['sql']!='':
            itmSql = itm['sql'].format(parm=sqlObjObj['dtGetJoinStr'])
            print(itmSql)
            print(sqlObjObj['sqlParmObj'])
            if itm['sqlType']==1:
                print(888)
                db_data,err  = dbTransactionCurd.dbListJon(db,itmSql,sqlObjObj['sqlParmObj'])
                if err!=None:

                    return None,err,db
                obj['a' + str(itm['id'])+'_data'] = db_data
            elif itm['sqlType']==2:
                db_data, err = dbTransactionCurd.dbOneJson(db, itmSql,sqlObjObj['sqlParmObj'])
                if err!=None:
                    return None,err,db
                obj['a' + str(itm['id'])+'_data'] = db_data
            elif itm['sqlType'] == 3 or itm['sqlType'] == 4 or itm['sqlType'] == 5:
                _, err = dbTransactionCurd.dbCPD(db, itmSql,sqlObjObj['sqlParmObj'])
                print(err)
                if err != None:
                    print('退出了')
                    return None, err, db
                obj['a' + str(itm['id']) + '_data']='操作成功'
            else:
                return obj, '操作类型操作错误',db
        else:
            obj['a' + str(itm['id']) + '_data'] = None
        if len(itm['children'])>0:
            obj['a' + str(itm['id'])] = {}
            _,err,db = recursionListOrigin(db,itm['children'],sqlObjObj,obj['a' + str(itm['id'])])
            if err!=None:
                return None, err, db
    return obj,None,db


